<script setup>
import { ref } from 'vue'

const templates = ref([
  {
    id: 1,
    name: '动态波浪',
    category: '自然',
    author: '声波工作室',
    price: 0,
    downloads: 2580,
    rating: 4.8,
    reviews: 326,
    preview: 'https://images.pexels.com/photos/1762851/pexels-photo-1762851.jpeg?auto=compress&cs=tinysrgb&w=1600',
    tags: ['波浪', '流体', '渐变'],
    description: '流畅的波浪动画效果，适合表现舒缓的音乐',
    features: [
      '多重波浪叠加',
      '可调节波幅',
      '自定义颜色',
      '速度控制'
    ]
  },
  {
    id: 2,
    name: '星空粒子',
    category: '空间',
    author: '夜光创意',
    price: 29,
    downloads: 1860,
    rating: 4.9,
    reviews: 245,
    preview: 'https://images.pexels.com/photos/1694000/pexels-photo-1694000.jpeg?auto=compress&cs=tinysrgb&w=1600',
    tags: ['星空', '粒子', '梦幻'],
    description: '梦幻的星空粒子效果，随音乐律动的星辰',
    features: [
      '3D粒子系统',
      '星辰闪烁效果',
      '动态星轨',
      '深度感知'
    ]
  },
  {
    id: 3,
    name: '频谱律动',
    category: '数据',
    author: '声谱实验室',
    price: 19,
    downloads: 3240,
    rating: 4.7,
    reviews: 468,
    preview: 'https://images.pexels.com/photos/1763075/pexels-photo-1763075.jpeg?auto=compress&cs=tinysrgb&w=1600',
    tags: ['频谱', '数据', '科技'],
    description: '专业的音频频谱可视化效果，支持多种显示模式',
    features: [
      '多种频谱模式',
      '实时响应',
      '高精度分析',
      '自定义样式'
    ]
  }
])

const categories = ref([
  { id: 'all', name: '全部模板' },
  { id: 'nature', name: '自然' },
  { id: 'space', name: '空间' },
  { id: 'data', name: '数据' },
  { id: 'abstract', name: '抽象' }
])

const filters = ref({
  category: 'all',
  price: 'all',
  sort: 'popular'
})

const selectedTemplate = ref(null)

const showTemplateDetail = (template) => {
  selectedTemplate.value = template
}
</script>

<template>
  <div class="templates-container">
    <div class="templates-header">
      <div class="header-content">
        <h1>模板市场</h1>
        <div class="filter-controls">
          <div class="filter-group">
            <label>分类</label>
            <select v-model="filters.category">
              <option v-for="category in categories" 
                      :key="category.id" 
                      :value="category.id">
                {{ category.name }}
              </option>
            </select>
          </div>
          <div class="filter-group">
            <label>价格</label>
            <select v-model="filters.price">
              <option value="all">全部价格</option>
              <option value="free">免费</option>
              <option value="paid">付费</option>
            </select>
          </div>
          <div class="filter-group">
            <label>排序</label>
            <select v-model="filters.sort">
              <option value="popular">最受欢迎</option>
              <option value="newest">最新发布</option>
              <option value="rating">评分最高</option>
            </select>
          </div>
        </div>
      </div>
      <div class="search-bar">
        <input type="text" placeholder="搜索模板名称、标签或作者...">
        <button class="search-btn">搜索</button>
      </div>
    </div>

    <div class="templates-grid">
      <div 
        v-for="template in templates"
        :key="template.id"
        class="template-card"
        @click="showTemplateDetail(template)"
      >
        <div class="template-preview">
          <img :src="template.preview" :alt="template.name">
          <div class="template-overlay">
            <button class="preview-btn">预览效果</button>
          </div>
        </div>
        <div class="template-info">
          <div class="template-header">
            <h3>{{ template.name }}</h3>
            <span class="price">{{ template.price === 0 ? '免费' : `￥${template.price}` }}</span>
          </div>
          <p class="author">作者：{{ template.author }}</p>
          <div class="template-stats">
            <span class="downloads">{{ template.downloads }} 次下载</span>
            <div class="rating">
              <span class="stars">★★★★★</span>
              <span class="rating-value">{{ template.rating }}</span>
              <span class="reviews">({{ template.reviews }})</span>
            </div>
          </div>
          <div class="template-tags">
            <span 
              v-for="(tag, index) in template.tags"
              :key="index"
              class="tag"
            >
              {{ tag }}
            </span>
          </div>
        </div>
      </div>
    </div>

    <!-- 模板详情弹窗 -->
    <div v-if="selectedTemplate" class="template-detail-modal" @click="selectedTemplate = null">
      <div class="modal-content" @click.stop>
        <button class="close-btn" @click="selectedTemplate = null">×</button>
        <div class="template-detail">
          <div class="detail-preview">
            <img :src="selectedTemplate.preview" :alt="selectedTemplate.name">
            <button class="preview-btn large">预览效果</button>
          </div>
          <div class="detail-info">
            <div class="detail-header">
              <div class="title-section">
                <h2>{{ selectedTemplate.name }}</h2>
                <div class="price-tag">
                  {{ selectedTemplate.price === 0 ? '免费' : `￥${selectedTemplate.price}` }}
                </div>
              </div>
              <p class="author">由 {{ selectedTemplate.author }} 创作</p>
            </div>
            
            <div class="stats-section">
              <div class="stat-item">
                <span class="stat-icon">⭐</span>
                <span class="stat-value">{{ selectedTemplate.rating }}</span>
                <span class="stat-label">评分</span>
              </div>
              <div class="stat-item">
                <span class="stat-icon">⬇️</span>
                <span class="stat-value">{{ selectedTemplate.downloads }}</span>
                <span class="stat-label">下载</span>
              </div>
              <div class="stat-item">
                <span class="stat-icon">💬</span>
                <span class="stat-value">{{ selectedTemplate.reviews }}</span>
                <span class="stat-label">评价</span>
              </div>
            </div>

            <div class="description-section">
              <h3>模板说明</h3>
              <p>{{ selectedTemplate.description }}</p>
            </div>

            <div class="features-section">
              <h3>主要特性</h3>
              <ul>
                <li v-for="(feature, index) in selectedTemplate.features" 
                    :key="index">
                  {{ feature }}
                </li>
              </ul>
            </div>

            <div class="action-buttons">
              <button class="download-btn">
                {{ selectedTemplate.price === 0 ? '立即下载' : '购买并下载' }}
              </button>
              <button class="preview-action-btn">在线预览</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.templates-container {
  min-height: calc(100vh - 72px);
  padding: 20px;
  max-width: 1800px;
  margin: 0 auto;
}

.templates-header {
  background: white;
  border-radius: 16px;
  padding: 32px;
  margin-bottom: 32px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.templates-header h1 {
  font-size: 28px;
  background: linear-gradient(45deg, #6366f1, #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.filter-controls {
  display: flex;
  gap: 24px;
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.filter-group label {
  font-size: 14px;
  color: #666;
}

.filter-group select {
  padding: 8px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  outline: none;
  min-width: 160px;
}

.search-bar {
  display: flex;
  gap: 16px;
}

.search-bar input {
  flex: 1;
  padding: 12px 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  outline: none;
  font-size: 16px;
}

.search-btn {
  padding: 12px 24px;
  background: #6366f1;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.search-btn:hover {
  background: #4f46e5;
}

.templates-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  gap: 24px;
}

.template-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: transform 0.3s;
}

.template-card:hover {
  transform: translateY(-4px);
}

.template-preview {
  position: relative;
  aspect-ratio: 16/9;
  overflow: hidden;
}

.template-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.template-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s;
}

.template-card:hover .template-overlay {
  opacity: 1;
}

.preview-btn {
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.9);
  border: none;
  border-radius: 8px;
  color: #6366f1;
  cursor: pointer;
  transition: transform 0.3s;
}

.preview-btn:hover {
  transform: scale(1.05);
}

.template-info {
  padding: 20px;
}

.template-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.template-header h3 {
  font-size: 18px;
  color: #4a4a4a;
}

.price {
  padding: 4px 12px;
  background: #f0f9ff;
  color: #0284c7;
  border-radius: 16px;
  font-size: 14px;
}

.author {
  color: #666;
  font-size: 14px;
  margin-bottom: 12px;
}

.template-stats {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.downloads {
  color: #666;
  font-size: 14px;
}

.rating {
  display: flex;
  align-items: center;
  gap: 4px;
}

.stars {
  color: #fbbf24;
  font-size: 14px;
}

.rating-value {
  font-weight: 500;
  color: #4a4a4a;
}

.reviews {
  color: #666;
  font-size: 14px;
}

.template-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  padding: 4px 12px;
  background: #f6f7fb;
  border-radius: 16px;
  font-size: 12px;
  color: #666;
}

.template-detail-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 16px;
  width: 90%;
  max-width: 1200px;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
}

.close-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  background: none;
  border: none;
  font-size: 24px;
  color: #666;
  cursor: pointer;
  z-index: 1;
}

.template-detail {
  display: grid;
  grid-template-columns: 1fr 400px;
  gap: 32px;
}

.detail-preview {
  position: relative;
}

.detail-preview img {
  width: 100%;
  height: auto;
}

.preview-btn.large {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 16px 32px;
  font-size: 18px;
}

.detail-info {
  padding: 32px;
}

.detail-header {
  margin-bottom: 24px;
}

.title-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.title-section h2 {
  font-size: 24px;
  color: #4a4a4a;
}

.price-tag {
  padding: 8px 16px;
  background: #f0f9ff;
  color: #0284c7;
  border-radius: 20px;
  font-weight: 500;
}

.stats-section {
  display: flex;
  gap: 32px;
  margin-bottom: 24px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 12px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stat-icon {
  font-size: 24px;
}

.stat-value {
  font-weight: 500;
  color: #4a4a4a;
}

.stat-label {
  font-size: 12px;
  color: #666;
}

.description-section,
.features-section {
  margin-bottom: 24px;
}

.description-section h3,
.features-section h3 {
  font-size: 18px;
  color: #4a4a4a;
  margin-bottom: 12px;
}

.description-section p {
  color: #666;
  line-height: 1.6;
}

.features-section ul {
  list-style: none;
  padding: 0;
}

.features-section li {
  color: #666;
  padding: 8px 0;
  padding-left: 24px;
  position: relative;
}

.features-section li::before {
  content: '✓';
  position: absolute;
  left: 0;
  color: #6366f1;
}

.action-buttons {
  display: flex;
  gap: 16px;
}

.download-btn,
.preview-action-btn {
  flex: 1;
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 16px;
}

.download-btn {
  background: #6366f1;
  color: white;
}

.download-btn:hover {
  background: #4f46e5;
}

.preview-action-btn {
  background: #f6f7fb;
  color: #6366f1;
}

.preview-action-btn:hover {
  background: #e5e7eb;
}

@media (min-width: 1920px) {
  .templates-grid {
    grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  }
}
</style> 